﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "PointSnapStrategy.h"
#include "IElement.h"
#include "IModelView.h"
#include "IDirectShape.h"
#include "Vector3dUtils.h"
#include "GraphicsNodeId.h"
#include "IGraphicsPoint.h"
#include "IGenericElement.h"
#include "IElementModelShape.h"
#include "IElementSnapStrategy.h"
#include "IPureGraphicsElement.h"
#include "IElementSnapComponent.h"
#include "IGraphicsElementShape.h"
#include "IInteractionSnapManager.h"
#include "GraphicsNodeAndTransform.h"
#include "IGraphicsNodePathCollector.h"
#include "ElementSnapStrategyRegister.h"

#include "CmdSnapModuleEntry.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

// 远程捕捉自定义点为空
std::vector<gcmp::RemoteSnapData> PointSnapStrategy::GetRemoteSnappableGeometry(const IModelView* pModelView, const IElement* pElement) const
{
    return std::vector<gcmp::RemoteSnapData>();
}

/// 获得构件中的IGraphicsPoint，并创建自定义捕捉点
void PointSnapStrategy::GetLocalCustomPointSnapData(const gcmp::IElement* pElement, const gcmp::IModelView* pModelView, std::vector<gcmp::LocalCustomPointSnapData>& snapDatas) const
{
    IDocument* pDocument = pModelView->GetDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDocument, L"pDocument为空",L"GDMP",L"2023-12-30");

    // 获得构件的IGraphicsElementShape
    const IElementModelShape* pElementModelShape = pElement->GetElementModelShape();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElementModelShape, L"pElementModelShape为空",L"GDMP",L"2023-12-30");
    const IGraphicsElementShape *pGraphicsElementShape = pElementModelShape->GetGraphicsElementShape();

    if (pGraphicsElementShape == nullptr)
    {
        return;
    }

    // 用IGraphicsNodePathCollector遍历IGraphicsElementShape并获得所有IGraphicsPoint
    OwnerPtr<IGraphicsNodePathCollector> opCollector = IGraphicsNodePathCollector::Create(pDocument, pGraphicsElementShape);
    DBG_WARN_AND_RETURN_VOID_UNLESS(opCollector, L"opCollector为空",L"GDMP",L"2023-12-30");
    OwnerPtr<PointGraphicsPointPathTester> opTester = NEW_AS_OWNER_PTR(PointGraphicsPointPathTester);
    DBG_WARN_AND_RETURN_VOID_UNLESS(opTester, L"opTester为空",L"GDMP",L"2023-12-30");
    OwnerPtr<IGraphicsNodePathCollection> opGraphicsNodePathCollection = opCollector->CollectGraphicsNodes(opTester.get());
    DBG_WARN_AND_RETURN_VOID_UNLESS(opGraphicsNodePathCollection, L"opGraphicsNodePathCollection为空",L"GDMP",L"2023-12-30");

    // 设置捕捉光标
    std::wstring cursorPath = L"";
    if (CmdSnapModuleEntry::DoCustomizeCursor())
    {
        cursorPath = L":/images/Cursor/cross_big.png";
    }

    // 根据IGraphicsPoint创建自定义捕捉点
    for (auto &itor : opGraphicsNodePathCollection->GetPaths())
    {
        // 获得点
        GraphicsNodeAndTransform lastNode = itor->GetLastGraphicsNodeAndTransform();
        const IGraphicsNode* pGNode = lastNode.m_wpGraphicsNode.Get();
        DBG_WARN_AND_CONTINUE_UNLESS(pGNode, L"pGNode为空",L"GDMP",L"2023-12-30");
        const IGraphicsPoint* pGPoint = quick_cast<IGraphicsPoint>(pGNode);
        DBG_WARN_AND_CONTINUE_UNLESS(pGPoint, L"pGPoint为空",L"GDMP",L"2023-12-30");
        Vector3d point = pGPoint->GetPoint();

        // 并对点做坐标变换到世界坐标系
        Vector3d transformPoint = Vector3dUtils::TransformPoint(point, *lastNode.m_opTransfrom.get());

        // 创建自定义点，并塞入返回值
        LocalCustomPointSnapData snapData = LocalCustomPointSnapData(SnapTypeForCustomPoint::CustomPoint, 
            transformPoint, L"自定义点", {}, cursorPath);
        snapDatas.emplace_back(snapData);
    }
}

/// 在遍历构件的图形节点时，过滤IGraphicsPoint
bool Sample::PointGraphicsPointPathTester::IsDesiredGraphicsNodePath(const gcmp::IGraphicsNodePath & nodePath) const
{
    GraphicsNodeAndTransform lastNode = nodePath.GetLastGraphicsNodeAndTransform();
    const IGraphicsNode* pGNode = lastNode.m_wpGraphicsNode.Get();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pGNode, L"pGNode为空",L"GDMP",L"2023-12-30");
    bool isGPoint = GraphicsNodeType::Point3d == pGNode->GetType();
    return isGPoint;
}

REGISTER_ELEMENT_SNAP_STRATEGY(IDirectShape, PointSnapStrategy);

